package com.example.neusoft.zyfypt_wk.model;


import com.example.neusoft.zyfypt_wk.bean.ArticleBean;
import com.example.neusoft.zyfypt_wk.bean.LoginBean;
import com.example.neusoft.zyfypt_wk.iface.ArticleListener;
import com.example.neusoft.zyfypt_wk.iface.Articleiface;
import com.example.neusoft.zyfypt_wk.iface.LoginListener;
import com.example.neusoft.zyfypt_wk.iface.Loginiface;
import com.example.neusoft.zyfypt_wk.service.ArticleService;
import com.example.neusoft.zyfypt_wk.service.UserService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Neusoft on 2017/4/20.
 */
//model  获取文章数据
public class LoginModel  implements Loginiface{
    private Retrofit retrofit;
    private String BASEURL
            ="http://amicool.neusoft.edu.cn/";
     //构造函数
    public LoginModel()
    {   //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getLoginResult(String username, String pass, final LoginListener loginListener) {
        //使用Retrofit----2
        UserService userService
                =retrofit.create(UserService.class);
        Call<LoginBean> call
                =userService.login(username,pass);
        //使用Retrofit----3
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if(response.isSuccessful() && response!=null)
                {  loginListener.onResponse(response.body());
                }
                else   loginListener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                loginListener.onFail(t.toString());
            }
        });
    }
}
